package itcast.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import itcast.domain.User;
import itcast.service.UserService;
import itcast.service.UserServiceimpl;

/**
 * Servlet implementation class UpdateuserServlet
 */
@WebServlet("/updateuserServlet")
public class UpdateuserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateuserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//doGet(request, response);
		//1、设置编码
		request.setCharacterEncoding("utf-8");
		//2、获取修改后的数据
		int id=Integer.parseInt(request.getParameter("id"));
		String username=request.getParameter("username");
		String name=request.getParameter("name");
		String gender=request.getParameter("gender");
		int age=Integer.parseInt(request.getParameter("age"));
		String address=request.getParameter("address");
		String qq=request.getParameter("qq");
		String email=request.getParameter("email");
		//3、封装
		User user= new User();
		user.setUserid(id);
		user.setUsername(username);
		user.setName(name);
		user.setGender(gender);
		user.setAge(age);
		user.setAddress(address);
		user.setQq(qq);
		user.setEmail(email);
		//3、调用Service查询对象
		UserService service=new UserServiceimpl();
		service.updateuser(user);
		//4、跳转到修改后的页面
		System.out.println("修改了");
		//5、返回修改的对应页数
		System.out.println(request.getHeader("Referer"));
	//	response.sendRedirect(request.getHeader("Referer"));
		
		response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
		
	}

}
